﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Ccit.ClassLibrary;

namespace Ccit.Utils
{
    public class XmlDataHandle
    {
        /// <summary>
        ///		创建一个NOBR节点
        /// </summary>
        /// <param name="oDom">
        ///		以ref方式传入的DomDocument对象
        /// </param>
        /// <returns></returns>
        public static XmlNode RetNobrNode(ref XmlDocument oDom)
        {
            XmlNode oNode = oDom.CreateNode(XmlNodeType.Element, "nobr", "");
            return oNode;
        }

        /// <summary>
        ///		为oNode节点增加Id属性
        /// </summary>
        /// <param name="oDom">
        ///		以ref方式传入的DomDocument对象
        /// </param>
        /// <param name="oNewNode">
        ///		以ref方式传入的需要增加Id属性的XmlNode对象
        /// </param>
        /// <param name="sFldId">
        ///		控件的Id属性名称
        /// </param>
        public static void AppendIdAttribute(ref XmlDocument oDom, ref XmlNode oNewNode, string sFldId)
        {
            XmlNode oIdAttrNode = oDom.CreateNode(XmlNodeType.Attribute, "id", "");
            oIdAttrNode.Value = sFldId;
            oNewNode.Attributes.SetNamedItem(oIdAttrNode);
        }

        /// <summary>
        ///		为oNewNode添加CssClass属性
        /// </summary>
        /// <param name="oDom">
        ///		以ref方式传送的DomDocument对象
        /// </param>
        /// <param name="oNewNode">
        ///		以ref方式传送的需要添加CssClass的XmlNode对象
        /// </param>
        /// <param name="sClassValue">
        ///		CssClass属性值
        /// </param>
        public static void AppendCssClassAttribute(ref XmlDocument oDom, ref XmlNode oNewNode, string sClassValue)
        {
            XmlNode oClassNode;
            oClassNode = oDom.CreateNode(XmlNodeType.Attribute, "class", "");
            oClassNode.Value = sClassValue;
            oNewNode.Attributes.SetNamedItem(oClassNode);
        }

        public static void AppendSrcAttribute(ref XmlDocument oDom, ref XmlNode oNewNode, string sSrcValue)
        {
            XmlNode oSrcNode;
            oSrcNode = oDom.CreateNode(XmlNodeType.Attribute, "src", "");
            oSrcNode.Value = sSrcValue;
            oNewNode.Attributes.SetNamedItem(oSrcNode);
        }

        public static void AppendBorderAttribute(ref XmlDocument oDom, ref XmlNode oNewNode, string sBorderValue)
        {
            XmlNode oBorderNode;
            oBorderNode = oDom.CreateNode(XmlNodeType.Attribute, "src", "");
            oBorderNode.Value = sBorderValue;
            oNewNode.Attributes.SetNamedItem(oBorderNode);
        }
        /////////////////////////////////////////////////////////////
        ///为oNode节点增加Value属性
        ///参数说明:	
        /////////////////////////////////////////////////////////////
        public static void AppendValueAttribute(ref XmlDocument oDom, ref XmlNode oNewNode, string sValue)
        {
            XmlNode oValueNode;
            oValueNode = oDom.CreateNode(XmlNodeType.Attribute, "value", "");
            oValueNode.Value = sValue;
            oNewNode.Attributes.SetNamedItem(oValueNode);
        }

        /// <summary>
        ///		为oNode节点增加MaxLength属性
        /// </summary>
        /// <param name="oDom">
        ///		以ref方式传入的DomDocument对象
        /// </param>
        /// <param name="oNewNode">
        ///		以ref方式传入的需要增加MaxLength属性的XmlNode对象
        /// </param>
        /// <param name="sLength">
        ///		长度值
        /// </param>
        public static void AppendMaxLenAttribute(ref XmlDocument oDom, ref XmlNode oNewNode, string sLength)
        {
            XmlNode oLengthAttrNode = oDom.CreateNode(XmlNodeType.Attribute, "maxlength", "");
            oLengthAttrNode.Value = sLength;
            oNewNode.Attributes.SetNamedItem(oLengthAttrNode);
        }

        /// <summary>
        /// 添加样式属性 style
        /// </summary>
        /// <param name="oDom"></param>
        /// <param name="oNewNode"></param>
        /// <param name="Style"></param>
        /// <param name="Type"></param>
        public static void AppendStyleAttribute(ref XmlDocument oDom, ref XmlNode oNewNode, string Style, string Type)
        {
            if (Type == "0")
            {
                if (Style != "")
                {
                    XmlNode oStyleAttrNode = (null != oNewNode.Attributes.GetNamedItem("style")) ? oNewNode.Attributes.GetNamedItem("style") : oDom.CreateNode(XmlNodeType.Attribute, "style", "");
                    if (oStyleAttrNode.Value == string.Empty)
                        oStyleAttrNode.Value += Style;
                    else
                        oStyleAttrNode.Value += ";" + Style;
                    oNewNode.Attributes.SetNamedItem(oStyleAttrNode);
                }
            }
            else if (Type == "1")
            {
                AppendStyleAttribute(ref oDom, ref oNewNode, Style);
            }
        }

        ////////////////////////////////////////////////////////////////////////////////////
        ///为oNode节点增加style属性,目前只有Width和Height属性,为预留接口,参数采用XML格式传送
        ///参数结构如下:
        ///<StyleList><Style><Width>200px</Width><Height>150px</Height></Style></StyleList>
        ////////////////////////////////////////////////////////////////////////////////////
        public static void AppendStyleAttribute(ref XmlDocument oDom, ref XmlNode oNewNode, string strXML)
        {
            XmlDocument oStyleDom = new XmlDocument();
            if (strXML != "")
            {
                oStyleDom.LoadXml(strXML);
                XmlNode oStyleAttrNode = (null != oNewNode.Attributes.GetNamedItem("style")) ? oNewNode.Attributes.GetNamedItem("style") : oDom.CreateNode(XmlNodeType.Attribute, "style", "");
                XmlNodeList oStyleNodeList = oStyleDom.GetElementsByTagName("Style")[0].ChildNodes;
                int iNodeLength = oStyleNodeList.Count;
                for (int i = 0; i < iNodeLength; i++)
                {
                    if (oStyleAttrNode.Value == string.Empty)
                    {
                        if (oStyleNodeList[i].Name.ToUpper() == "WIDTH" || oStyleNodeList[i].Name.ToUpper() == "HEIGHT")
                            oStyleAttrNode.Value += oStyleNodeList[i].Name + ":" + oStyleNodeList[i].InnerText.Trim() + "px";
                        else
                            oStyleAttrNode.Value += oStyleNodeList[i].Name + ":" + oStyleNodeList[i].InnerText.Trim();
                    }
                    else
                    {
                        if (oStyleNodeList[i].Name.ToUpper() == "WIDTH" || oStyleNodeList[i].Name.ToUpper() == "HEIGHT")
                            oStyleAttrNode.Value += ";" + oStyleNodeList[i].Name + ":" + oStyleNodeList[i].InnerText.Trim() + "px";
                        else
                            oStyleAttrNode.Value += ";" + oStyleNodeList[i].Name + ":" + oStyleNodeList[i].InnerText.Trim();
                    }

                }
                oNewNode.Attributes.SetNamedItem(oStyleAttrNode);
            }
        }

        public static void AppendStyleAttribute(ref XmlDocument oDom, ref XmlNode oNewNode, string strXML,List<MsKeyValue> lstFixSign)
        {
            bool widthfix = true;
            bool heightfix = true;
            if(null == lstFixSign || lstFixSign.Count == 0)
            {
                widthfix = true;
                heightfix = true;
            }
            else if (lstFixSign.Count == 1)
            {
                MsKeyValue dict = lstFixSign[0];
                if (dict.key.ToUpper() == "WIDTH")
                    widthfix = (dict.val == "1") ? true : false;
                if (dict.key.ToUpper() == "HEIGHT")
                    heightfix = (dict.val == "1") ? true : false;
            }
            else if (lstFixSign.Count == 0)
            {
                foreach (MsKeyValue eDict in lstFixSign)
                {
                    if (eDict.key.ToUpper() == "WIDTH")
                        widthfix = (eDict.val == "1") ? true : false;
                    if (eDict.key.ToUpper() == "HEIGHT")
                        heightfix = (eDict.val == "1") ? true : false;
                }
            }
            else { }

            XmlDocument oStyleDom = new XmlDocument();
            if (strXML != "")
            {
                oStyleDom.LoadXml(strXML);
                XmlNode oStyleAttrNode = (null != oNewNode.Attributes.GetNamedItem("style")) ? oNewNode.Attributes.GetNamedItem("style") : oDom.CreateNode(XmlNodeType.Attribute, "style", "");
                XmlNodeList oStyleNodeList = oStyleDom.GetElementsByTagName("Style")[0].ChildNodes;
                int iNodeLength = oStyleNodeList.Count;
                for (int i = 0; i < iNodeLength; i++)
                {
                    if (oStyleAttrNode.Value == string.Empty)
                    {
                        oStyleAttrNode.Value += oStyleNodeList[i].Name + ":" + oStyleNodeList[i].InnerText.Trim();
                        if (oStyleNodeList[i].Name.ToUpper() == "WIDTH" && widthfix)
                        {                            
                           oStyleAttrNode.Value += "px";
                        }
                        
                        if (oStyleNodeList[i].Name.ToUpper() == "HEIGHT" && heightfix)
                        {
                           oStyleAttrNode.Value += "px";
                        }
                        

                        //if (oStyleNodeList[i].Name.ToUpper() == "WIDTH" || oStyleNodeList[i].Name.ToUpper() == "HEIGHT")
                        //    oStyleAttrNode.Value += oStyleNodeList[i].Name + ":" + oStyleNodeList[i].InnerText.Trim() + "px";
                        //else
                        //    oStyleAttrNode.Value += oStyleNodeList[i].Name + ":" + oStyleNodeList[i].InnerText.Trim();
                    }
                    else
                    {
                        oStyleAttrNode.Value += ";" + oStyleNodeList[i].Name + ":" + oStyleNodeList[i].InnerText.Trim();
                        if (oStyleNodeList[i].Name.ToUpper() == "WIDTH" && widthfix)
                        {
                            oStyleAttrNode.Value += "px";
                        }

                        if (oStyleNodeList[i].Name.ToUpper() == "HEIGHT" && heightfix)
                        {
                            oStyleAttrNode.Value += "px";
                        }
                        
                        //if (oStyleNodeList[i].Name.ToUpper() == "WIDTH" || oStyleNodeList[i].Name.ToUpper() == "HEIGHT")
                        //    oStyleAttrNode.Value += ";" + oStyleNodeList[i].Name + ":" + oStyleNodeList[i].InnerText.Trim() + "px";
                        //else
                        //    oStyleAttrNode.Value += ";" + oStyleNodeList[i].Name + ":" + oStyleNodeList[i].InnerText.Trim();
                    }

                }
                oNewNode.Attributes.SetNamedItem(oStyleAttrNode);
            }
        }

        /// <summary>
        /// 创建hidden 控件
        /// </summary>
        /// <param name="domObj"></param>
        /// <param name="ctrlId"></param>
        /// <returns></returns>
        public static XmlNode CreateHiddenControlNode(ref XmlDocument domObj,string ctrlId)
        {
            XmlNode oHdnNode;
            XmlNode oHdnAttrNode;
            string sHdnId = ctrlId;  //写死值

            oHdnNode = domObj.CreateNode(XmlNodeType.Element, "input", "");
            oHdnAttrNode = domObj.CreateNode(XmlNodeType.Attribute, "type", "");
            oHdnAttrNode.Value = "hidden";
            oHdnNode.Attributes.SetNamedItem(oHdnAttrNode);

            /* 添加id属性 */
            AppendIdAttribute(ref domObj, ref oHdnNode, sHdnId);

            /* 添加值属性 */
            AppendValueAttribute(ref domObj, ref oHdnNode, "");
            return oHdnNode;
        }
    }
}
